<template>
  <div v-loading="loading">
    <ul class="list-ul" v-if="list.length > 0">
      <li
        v-for="item of list"
        :key="item.id"
        @click="goPay(item)"
        :class="activeAmount == item.price ? 'active' : ''"
        :style="item.checked ? 'background: #aab6e8' : ''"
      >
        {{ item.currency_symbol }} {{ item.price }}
      </li>
    </ul>
    <p v-else class="noData">{{ $t("noData") }}</p>
    <!-- <div class="rechargeSearch">
      <el-input
        v-model="amount"
        type="number"
        placeholder="请输入充值金额,最小输入10"
        @change="handleInput"
      ></el-input>
    </div> -->
    <div class="pay-way-box" v-if="payWayShow">
      <div class="title">{{ $t("paymentMethod") }}</div>
      <ul class="pay-way-list">
        <li
          class="pay-way"
          v-for="(item, index) in channel"
          :key="index"
          @click="goShop(item.pay_channel)"
        >
          <div>
            {{ item.title }}
          </div>
        </li>
      </ul>
    </div>
  </div>
</template>
<script>
import {
  httpGetChannel,
  httpInfo,
  httpGoodsList,
  httpPlaceOrder,
} from "@/http/api";
import { formatMoney } from "@/utils/common";
export default {
  name: "recharge",
  data() {
    return {
      value: 0,
      listLoading: false,
      ways2: [], // 默认支付方式
      selectItem: {}, // 选中的支付方式
      deleModal: false, // 删除弹窗是否展示
      paysAjax: false,
      assets_total: 0,
      disabled: true,
      list: [],
      goods_tip: "",
      pages: {
        // 分页信息
        page: 1,
        total: 0,
        nodata: false,
      },
      amount: null,
      channel: [],
      payWayShow: false,
      activeAmount: "",
      payAmount: null,
      payWay: "",
      loading: false,
    };
  },
  created() {
    this.getHttpGoodsList();
    this.getHttpInfo();
  },
  methods: {
    formatMoney,
    handleInput(value) {
      // 如果输入值小于 10，则将其设为 10
      if (value < 10) {
        this.amount = 10;
      } else {
        this.amount = value;
      }
    },
    goPay(item) {
      this.payAmount = item;
      this.getHttpGetChannel();
      this.payWayShow = true;
      this.activeAmount = item.price;
    },
    async goShop(payWay) {
      this.payWay = payWay;
      const { goods_id } = this.payAmount;
      const res = await httpPlaceOrder({ goods_id, pay_channel: this.payWay });
      if (res.code === 0) {
        if (this.payWay.includes("USDT")) {
          this.$router.push({
            name: "payment",
            params: {
              item: JSON.stringify(res.data),
            },
          });
        } else {
          window.open(res.data.jump_url);
        }
      }
    },
    async getHttpInfo() {
      let res = await httpInfo();
      if (res.code == 0) {
        this.assets_total = res.data?.diamond || 0;
      }
    },
    async getHttpGoodsList() {
      let params = {
        page: this.pages.page,
      };
      this.loading = true;
      const res = await httpGoodsList(params);
      this.loading = false;
      if (res.code == 0) {
        this.list = [...this.list, ...(res.data.list || [])];
        this.goods_tip = res.data?.goods_tip.replace(/\n/g, "<br/>");
        this.pages.total = res.data.page.total_count;
        if (this.list.length < this.pages.total) this.pages.page++;
        if (!this.pages.total) this.pages.nodata = true;
      }
      this.listLoading = false;
    },
    // 获取支付通道
    async getHttpGetChannel() {
      let res = await httpGetChannel();
      if (res.code == 0) {
        this.channel = res.data || [];
        if (this.channel.length > 0) {
          this.payWay = this.channel[0].pay_channel;
        }
      }
    },
  },
};
</script>
<style lang="less" scoped>
.list-ul {
  display: grid;
  grid-template-columns: repeat(auto-fill, 20%);
  justify-content: space-between;
  gap: 10px;
  li {
    width: 100%;
    background: #e6e6e6;
    border-radius: 10px;
    padding: 15px;
    text-align: center;
    box-sizing: border-box;
    cursor: pointer;
  }
  .active {
    background: #efcd2e;
    color: #fff;
  }
}
.noData {
  text-align: center;
  font-size: 20px;
  color: #777;
}
.rechargeSearch {
  margin-top: 15px;
}

.pay-way-box {
  margin-top: 30px;
  .title {
    display: inline-block;
    font-size: 16px;
    height: 60px;
    flex-shrink: 0;
    font-weight: 500;
  }
  .pay-way-list {
    display: grid;
    grid-template-columns: repeat(auto-fill, 30%);
    justify-content: space-between;
    gap: 10px;
    .pay-way {
      width: 100%;
      height: 100%;
      background: #ced4f8;
      border-radius: 10px;
      padding: 10px;
      text-align: center;
      box-sizing: border-box;
      display: flex;
      justify-content: center;
      align-items: center;
      border-radius: 15px;
      margin-bottom: 20px;
      cursor: pointer;
    }
  }
}
</style>
